#!/bin/sh
# Job yaml can send below ENVIRONMENT VARIABLES to me
# - test
# - nr_task
# - nr_job
# - iterations
# - runtime
# END ENVIRONMENT VARIABLES

## REAIM is an updated and improved version of AIM 7 benchmark. It
## forks many processes called tasks, each of which concurrently runs
## in random order a set of subtests called jobs.  Each job exercises
## a different aspect of the operating system, such as disk-file
## operations, process creation, user virtual memory operations, pipe
## I/O, and compute-bound arithmetic loops.
##
## Homepage: https://sourceforge.net/projects/re-aim-7/

. $LKP_SRC/lib/debug.sh
. $LKP_SRC/lib/reproduce-log.sh

[ -n "$test" ] || die "test is empty"
[ -n "$runtime" ] || [ -n "$iterations" ] || die "runtime or iterations must be specified"

nr_job=${nr_job:-100}

run_reaim()
{
	log_cmd ./reaim -s$REAIM_STARTUSERS -e$REAIM_ENDUSERS -i$REAIM_INCREMENT -fworkfile.$test \
		-r${1:-1} -c./reaim.config -l$TMP_RESULT_ROOT/reaim_debug -j$nr_job |
		grep -v -e "^NSIG is "
}

run_for_time()
{
	start_time=$(date +%s)
	while :
	do
		run_reaim
		now=$(date +%s)
		[ $((now - start_time)) -gt "$runtime" ] && break
	done
}

run_for_iterations()
{
	run_reaim $iterations
}

cd $BENCHMARK_ROOT/reaim || exit

if [ -z "$nr_task" ]; then
	REAIM_STARTUSERS=1
	REAIM_ENDUSERS=$((nr_cpu*4))
	REAIM_INCREMENT=$((nr_cpu/4))
	[ "$REAIM_INCREMENT" = 0 ] && REAIM_INCREMENT=1
else
	REAIM_STARTUSERS=$nr_task
	REAIM_ENDUSERS=$nr_task
	REAIM_INCREMENT=$nr_task
fi

REAIM_FILESIZE=10k
REAIM_POOLSIZE=1m

# setup workfile
if [ -f "data/workfile.$test" ]; then
	log_cmd cp data/workfile.$test .
else
	# create workfile onlive
	log_eval "echo 10 $test > workfile.$test"
fi

# setup config file
if [ -n "$mount_points" ]; then
	REAIM_TESTDIR="${mount_points%% *}"
else
	log_cmd mkdir -p /fs/shm
	log_cmd umount /fs/shm > /dev/null 2>&1
	log_cmd mount -t tmpfs tmpfs /fs/shm
	REAIM_TESTDIR="/fs/shm"
fi

log_eval "echo FILESIZE $REAIM_FILESIZE > reaim.config"
log_eval "echo POOLSIZE $REAIM_POOLSIZE >> reaim.config"
log_eval "echo DISKDIR $REAIM_TESTDIR  >> reaim.config"

if [ -n "$runtime" ]; then
	echo "runtime mode"
	run_for_time
else
	echo "iterations mode"
	iterations=${iterations:-1}
	run_for_iterations
fi
